var express = require('express')
var router = express.Router();
var pool = require('../config/mysqlconfig');
// 获取数据
// http://localhost:3000/info/all
router.get('/all',function(req,res){
    pool.getConnection(function(err,conn){
        if(err){
            console.log(err)
        }else{
            let sql = "select * from info"
            conn.query(sql,function(error,results){
                if(error){
                    console.log(error)
                }
                res.send(results)
                conn.release()
            })
        }
    })
})

// 添加数据
// http://localhost:3000/info/add
router.post("/add",function(req,res){
    pool.getConnection(function(err,conn){
        if(err){
            console.log(err)
        }else{
            let title = req.body.i_title;
            let subtitle = req.body.i_subtitle;
            let about = req.body.i_about;
            let create = new Date();
            let update = new Date();
            let data={
                i_title:title,
                i_subtitle:subtitle,
                i_about:about,
                i_createat:create,
                i_updatedat:update
            }
            let sql = "insert into info set ?"
            conn.query(sql,data,function(error,results){
                if(error){
                    console.log(error)
                }
                res.send(results)
                conn.release()
            })
        }
    })
})

// 删除数据
// http://localhost:3000/info/del
router.delete("/del",function(req,res){
    pool.getConnection(function(err,conn){
        if(err){
            console.log(err)
        }else{
            let id = req.body.id
            let sql = "delete from info where id=?"
            conn.query(sql,id,function(error,results){
                if(error){
                    console.log(error)
                }
                res.send(results)
                conn.release()
            })
        }
    })
})

// 更新数据
// http://localhost:3000/info/update
router.put("/update",function(req,res){
    pool.getConnection(function(err,conn){
        if(err){
            console.log(err)
        }else{
            let title = req.body.i_title;
            let subtitle = req.body.i_subtitle;
            let about = req.body.i_about;
            
            let update = new Date();
            let id = req.body.id;
            let data=[title,subtitle,about,new Date(update),id]
            let sql = "update info set i_title=?,i_subtitle=?,i_about=?,i_updatedat=? where id=?"
            conn.query(sql,data,function(error,results){
                if(error){
                    console.log(error)
                }
                res.send(results)
                conn.release()
            })
        }
    })
})

// 查找数据
// http://localhost:3000/info/sel
router.post("/sel",function(req,res){
    pool.getConnection(function(err,conn){
        if(err){
            console.log(err)
        }else{
            let id = req.body.id;
            let sql = "select * from info where id=?"
            conn.query(sql,id,function(error,results){
                if(error){
                    console.log(error)
                }
                res.send(results)
                conn.release()
            })
        }
    })
})
module.exports = router